﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using DAL;

namespace 包覆门.LineManager
{
    public partial class LineEditing : Form
    {
        public LineEditing()
        {
            InitializeComponent();
        }
        public bool IsNew { get; set; }
        public string EditingId { get; set; }
        private ZsLineDAL dal = new ZsLineDAL();
        ZsLine zll = null;
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateData()) return;
            ZsLine zl = new ZsLine();
            zl.zsbh = this.tbZsbh.Text;
            zl.zsmc = this.tbZsmc.Text;
            zl.zsdj = this.tbZsdj.Text;
            foreach(RadioButton rb in this.gbZslb.Controls){
                if (rb.Checked) {
                    zl.zslb = Convert.ToInt32( rb.Tag.ToString());
                    break;
                }
            }
            if (this.rbZtYes.Checked)
            {
                zl.zslb = 1;
                zl.xtcd = this.tbZtcd.Text;
                zl.oldPrice = this.tboldprice.Text;

            }
            else {
                zl.zslb = 0;
            }
            if (IsNew)
            {
                if (dal.SaveData(zl) > 0)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else 
            {
                zl.zid = Convert.ToInt32(EditingId);
                if (dal.UpdateData(zl) > 0)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        //验证
        private bool ValidateData() {
            bool flag = true;
            string zsbh = this.tbZsbh.Text.Trim() ;
            string zsmc = this.tbZsmc.Text.Trim();
            string zsdj = this.tbZsdj.Text.Trim();
            string ztcd = this.tbZtcd.Text.Trim();
            string oldprice = this.tboldprice.Text.Trim();
            if (string.IsNullOrWhiteSpace(zsbh))
            {
                MessageBox.Show("编号不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(zsmc))
            {
                MessageBox.Show("名称不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(zsdj))
            {
                MessageBox.Show("单价不能为空");
                return false;
            }
            if (this.rbZtYes.Checked) {
                if (string.IsNullOrWhiteSpace(zsdj))
                {
                    MessageBox.Show("柱头尺寸不能为空");
                    return false;
                }
                if (string.IsNullOrWhiteSpace(oldprice))
                {
                    MessageBox.Show("柱头价格不能为空");
                    return false;
                }
            }
            return flag;
        }
        //加载数据
        private void LineEditing_Load(object sender, EventArgs e)
        {
            if (IsNew)
            {
                SetState(false);
            }
            else
            {
                zll = dal.GetById(EditingId);
                this.tbZsbh.Text = zll.zsbh;
                this.tbZsmc.Text = zll.zsmc;
                this.tbZsdj.Text = zll.zsdj;
                foreach (RadioButton rb in gbZslb.Controls) {
                    if (Convert.ToInt32(rb.Tag) == zll.zslb)
                    {
                        rb.Checked = true;
                    }
                    else 
                    {
                        rb.Checked = false;
                    }
                }
                if (zll.zslb == 1)
                {
                    SetState(true);
                    this.rbZtYes.Checked = true;
                    this.tbZtcd.Text = zll.xtcd;
                    this.tboldprice.Text = zll.oldPrice;
                }
                else 
                {
                    SetState(false);
                    this.rbZtNo.Checked = true;
                }
                
            }
        }
        //状态机
        public void SetState(bool flag) {
            this.tboldprice.Enabled = flag;
            this.tbZtcd.Enabled = flag;
        }

        private void rbZtNo_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbZtNo.Checked)
            {
                SetState(false);
                this.tbZtcd.Text = "";
                this.tboldprice.Text ="0";
            }
            else {
                SetState(true);
                if (!IsNew) {
                    this.tbZtcd.Text = zll.xtcd;
                    this.tboldprice.Text = zll.oldPrice;
                }
            }
        }
    }
}
